#include "ExpAST.h"
#include "Globe.h"
#include <iostream>
#include "parser.h"
#include "Globe.h"
#pragma warning(disable:4267)
using std::cout;
using std::endl;

unique_ptr<BaseExpAST> parser::parseVal()
{
    unique_ptr<ValAST>val(new ValAST);
    val->name = this->lexer->get_buffer();
    this->lexer->match(TOK::IDENTIFIER);
    return val;
}

void ValAST::Dump() const
{
    cout << "{\"type\":\"Val\",\"content\":\"" << this->name << "\"}";
}


bool ValAST::is_lval() const
{
    return CurrentValMgr->Lookup(this->name) && (CurrentValMgr->QueryEditability(this->name) == true);
}

cmd_val ValAST::get_address() const
{
    if (!CurrentValMgr->Lookup(this->name))
    {
        std::cerr << "unexpected identity " << this->name << endl;
        exit(-1);
    }
    cmd_val ret{};
    ret.offset = CurrentValMgr->QueryOffset(this->name);
    ret.regIdentity = CurrentValMgr->QueryOffsetRegister(this->name);
    ret.op = cmd_val::MEMORY;
    return ret;
}

void ValAST::Analyze() const
{
    cmd_code code1{};
    code1.op = cmd_code::MOV;
    code1.type1 = this->get_address();
    code1.type2 = CVR(EAX);
    CurrentFuncMgr->code_list.push_back(code1);
}